/**
 * 
 */
package com.huniu.links.hive;

import java.io.File;
import java.io.FileFilter;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.huniu.links.model.LinkXMLProperty;
import com.huniu.links.model.SeedXMLProperty;
import com.huniu.utility.DateUtil;

/**
 * @author zandy (chenheng)
 * 
 */
public class HiveProcessCenterImpl implements IHiveProcessCenter {

	private String seedFolder;
	private String linkFolder;

	private IScoutBeeBehaviorPool scoutPool ;
	private ILeaderBeeBehaviorPool leaderPool ;

	public void setSeedFolder(String seedFolder) {
		this.seedFolder = seedFolder;
	}

	public void setLinkFolder(String linkFolder) {
		this.linkFolder = linkFolder;
	}

	public void setScoutPool(IScoutBeeBehaviorPool scoutPool) {
		this.scoutPool = scoutPool;
	}

	public void setLeaderPool(ILeaderBeeBehaviorPool leaderPool) {
		this.leaderPool = leaderPool;
	}

	public void linkSeedDispatch() {
		File[] folders = getAvailableSeedFolders(seedFolder);
		List<File> fileList = new ArrayList<File>();
		for (File folder : folders) {
			File[] files = getAvailableSeedFiles(folder);
			for (File file : files) {
				fileList.add(file);
			}
		}
		scoutPool.setSeedFileList(fileList);
		scoutPool.setLinkFolder(linkFolder);
		scoutPool.execute();
	}

	public void gatherLinkDispatch() {
		File[] folders = getAvailableLinkFolders(linkFolder);
		List<File> fileList = new ArrayList<File>();
		for (File folder : folders) {
			File[] files = getAvailableLinkFiles(folder);
			for (File file : files) {
				fileList.add(file);
			}
		}
		leaderPool.setLinkFileList(fileList);
		leaderPool.setSeedFolder(seedFolder);
		leaderPool.execute();
	}

	private File[] getAvailableSeedFolders(String seedFolder) {
		return getAvailableFolders(seedFolder, SEED_FOLDER_PERIOD);
	}

	private File[] getAvailableLinkFolders(String linkFolder) {
		return getAvailableFolders(linkFolder, LINK_FOLDER_PERIOD);
	}

	private File[] getAvailableFolders(String strFolder, final int period) {
		File folder = new File(strFolder);
		File[] folders = folder.listFiles(new FileFilter() {
			public boolean accept(File folder) {
				String folderName = folder.getName();
				try {
					String strCurrentDate = DateUtil.convertDateToString(
							new Date(), DateUtil.DATE_FORMAT_1);
					long duration = DateUtil.calculateDuration(strCurrentDate,
							folderName, DateUtil.DATE_FORMAT_1,
							DateUtil.DATE_FORMAT_1);
					if (duration > period) {
						return false;
					} else {
						return true;
					}
				} catch (ParseException e) {
					return false;
				}
			}
		});
		return folders;
	}

	private File[] getAvailableSeedFiles(File folder) {
		String[] extensiones = {SeedXMLProperty.EXTENSION} ;
		return getAvailableFiles(folder,extensiones);
	}

	private File[] getAvailableLinkFiles(File folder) {
		String[] extensiones = {LinkXMLProperty.EXTENSION} ;
		return getAvailableFiles(folder,extensiones);
	}

	private File[] getAvailableFiles(File folder,final String[] extensiones) {
		File[] files = folder.listFiles(new FileFilter() {
			public boolean accept(File file) {
				String fileName = file.getName();
				for (String extension : extensiones) {
					if (fileName.toLowerCase().endsWith(extension)) {
						return true;
					}
				}
				return false;
			}
		});
		return files;
	}

}
